import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 操作管理
 * guopeng
 * 2017.11.27
 */
export class Operator{
  constructor(
    private operateId : string,//操作id
    private parentOperateId : string,//父操作id
    private operateName   : string,//操作名
    private operateUrl : string,//操作地址
    private operateDesc :string,//操作描述
    private operateStatus : string,//操作状态，0：启用，1：停用
    private operateOrder : string,//操作排序
    private createTime : string,//创建时间
    private updateTime : string,//更新时间
    private extensionInfo : string//拓展信息
  ){}
}

@Component({
  selector: 'app-operator',
  templateUrl: './operator.component.html',
  styleUrls: ['./operator.component.css']
})
export class OperatorComponent implements OnInit{
  private operateName   : string;//操作名
  private operateUrl : string;//操作地址
  private operateStatus : string;//操作状态，0：启用，1：停用
  errorMessage:string;//錯誤消息
  private list:Array<any>;
  public queryMessage:Operator = new Operator("","","","","","","","","","");
  public updateMessages:Operator = new Operator("","","","","","","","","","");
  public addMessages:Operator =  new Operator("","","","","","","","","","");
  private OperatorList : Operator;
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService) {

  }
  add(){
    this.addMessages =  new Operator("","","","","","","","","","");
    this.http.get(this.url+'/operate/queryOperateListByPage').subscribe(data => {
      this.OperatorList = data['records'];
    })
  }

  userMessage(event:any){
    let query = new Operator("","","","","","","","","","");
    let update = new Operator("","","","","","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
    this.addMessages =  new Operator("","","","","","","","","","");
    this.http.get(this.url+'/operate/queryOperateListByPage').subscribe(data => {
      this.OperatorList = data['records'];
    })
  }
  listMessage(info:any){
    let operateName = info.operateName== undefined ? '' : info.operateName ;
    let operateUrl = info.operateUrl== undefined ? '' : info.operateUrl;
    let operateStatus = info.operateStatus== undefined ? '' : info.operateStatus;
    this.http.get(this.url+'/operate/queryOperateListByPage?operateName='+operateName+'&operateUrl='+operateUrl+'&operateStatus='+operateStatus).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/operate/addOperate',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101115001'){
          this.errorMessage = "操作名为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115002'){
          this.errorMessage = "操作url为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115003'){
          this.errorMessage = "操作url已存在";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115004'){
          this.errorMessage = "操作状态为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115005'){
          this.errorMessage = "操作序号为空";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/operate/deleteOperate/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101115006'){
        this.errorMessage = "该操作不存在";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/operate/updateOperate',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101115001'){
          this.errorMessage = "操作名为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115002'){
          this.errorMessage = "操作url为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115003'){
          this.errorMessage = "操作url已存在";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115004'){
          this.errorMessage = "操作状态为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115005'){
          this.errorMessage = "操作序号为空";
          $("#error").modal('show');
        }else if(data['resultCode']=='101115006'){
          this.errorMessage = "该操作不存在";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let operateName = this.operateName== undefined ? '' : this.operateName ;
    let operateUrl = this.operateUrl== undefined ? '' : this.operateUrl;
    let operateStatus = this.operateUrl== undefined ? '' : this.operateStatus;
    let current = pageNo;
    this.http.get(this.url+'/operate/queryOperateListByPage?operateName='+operateName+'&operateUrl='+operateUrl+'&operateStatus='+operateStatus+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/operate/queryOperateListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}

